package projectmanagement.managementtool.MeetingServices;

import projectmanagement.managementtool.IdGenerator;
import projectmanagement.models.Meeting;
import projectmanagement.models.User;
import projectmanagement.models.Project;

import java.util.Calendar;
import java.util.List;

/**
 * MeetingServiceImpl provides concrete implementation of
 * functionality for MeetingService.
 *
 * @author Valera Rusakov
 */
public class MeetingServiceImp implements MeetingService {


    public Meeting createMeeting(String description, Calendar meetingStartTime, Calendar meetingDuration,
                                 Project project) {
        if (isValidTime(meetingStartTime)) {
            int id = IdGenerator.generate(Meeting.class);
            Meeting meeting = new Meeting(description, meetingStartTime, meetingDuration);
            project.getMeetingsMap().put(id, meeting);
            return meeting;
        } else
            return null;
    }

    public Meeting editMeeting(String description, Calendar meetingStartTime, Meeting meeting) {
        meeting.setMeetingDescription(description);
        meeting.setMeetingStartTime(meetingStartTime);
        return meeting;
    }

    public boolean assignUsersOnMeeting(List list, Meeting meeting) {
        return meeting.getUserList().addAll(list);
    }

    public String getMeetingDetails(Meeting meet) {
        return meet.toString();
    }

    public boolean assignUserOnMeeting(User user, Meeting meet) {
        return meet.getUserList().add(user);
    }

    public Meeting getMeetingById(Project project, int id) {
        if (project.getMeetingsMap().containsKey(id))
            return project.getMeetingsMap().get(id);
        else
            return null;
    }

    public boolean isValidTime(Calendar meetingStartTime) {
        Calendar currentTime = Calendar.getInstance();
        long start = meetingStartTime.getTimeInMillis();
        long current = currentTime.getTimeInMillis();
        if (current < start)
            return true;
        else
            return false;
    }
}
